package hhpoll.service.ws;

import hhpoll.service.context.RequestContext;

import org.apache.cxf.binding.soap.interceptor.SoapHeaderInterceptor;
import org.apache.cxf.configuration.security.AuthorizationPolicy;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Message;
import org.apache.log4j.Logger;

public class CXFRequestContextInterceptor extends SoapHeaderInterceptor {
	protected Logger log = Logger.getLogger(getClass());
	
	@Override public void handleMessage(Message message) throws Fault {
		// This is set by CXF
		AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
		// Get the current request context
		RequestContext ctxt = RequestContext.getContext();
		log.info("Got current request context: "+ctxt);
		// Set the username/password in the context
		if (policy != null) {
    		ctxt.setUsername(policy.getUserName());
    		ctxt.setPassword(policy.getPassword());
    		log.info("username="+policy.getUserName());
    		log.info("password="+policy.getPassword());
		} else {
    		log.info("Did not receive username/password");
    		ctxt.setUsername("");
    		ctxt.setPassword("");
		}
	}
}

